import { getRepository } from 'typeorm'
import { Student } from '../../entity/Student'
import { StudentDetail } from '../../entity/StudentDetails'
import { EventEmitter } from '../../helpers/EventEmitter'
import { CheckResultInter } from '../../routes'
import { MysqlService } from './MysqlService'

export class StudentService extends EventEmitter {
  public mysqlService!: MysqlService
  constructor(mysqlService: MysqlService) {
    super()
    this.mysqlService = mysqlService
  }

  public async createStudent(studentData: Student): Promise<CheckResultInter & { id?: number }> {
    const {
      address, name,
      fatherName, age,
      avatar, fatherPhoneNumber,
      motherName, motherPhoneNumber,
      motherWxchatNumber, fatherWechat, guardian, remark,
      height, weight, joinSchoolTime, shoeSize, studentNumber, sex, isPayTuition,
      isGainSchoolUniform, schoolType
    } = studentData
    const code = 4003
    if (!fatherName && !motherName) {
      return { msg: '父母名字不能都为空, 必须二者填其一', code }
    }

    if (!fatherPhoneNumber && !motherPhoneNumber) {
      return { msg: '父母名字不能都为空, 必须二者填其一', code }
    }

    if (!fatherWechat && !motherWxchatNumber) {
      return { msg: '父母微信不能都为空, 必须二者填其一', code }
    }
    const student = new Student()
    student.name = name
    student.weight = weight
    student.height = height
    student.joinSchoolTime = joinSchoolTime
    student.remark = remark || ''
    student.shoeSize = shoeSize
    student.sex = sex
    student.studentNumber = studentNumber
    student.motherPhoneNumber = motherPhoneNumber
    student.fatherPhoneNumber = fatherPhoneNumber
    student.fatherName = fatherName || ''
    student.motherName = motherName || ''
    student.fatherWechat = fatherWechat
    student.motherWxchatNumber = motherWxchatNumber
    student.isPayTuition = isPayTuition
    student.avatar = avatar
    student.address = address
    student.age = age
    student.guardian = guardian
    student.isGainSchoolUniform = isGainSchoolUniform
    student.schoolType = schoolType
    try {
      const { id } = await getRepository(Student).save(student)
      return { msg: '', code: 0, id }
    } catch (error) {
      console.error(error)
      return { code: 4005, msg: 'insert error [ createStudent ]' }
    }
  }

  public async studentList(offset: number, count: number, ): Promise<CheckResultInter & { list: Student[] }> {
    try {
      // take => limit; skip => offset
      const list = await getRepository(Student).find({
        take: count,
        skip: offset,
        select: [
          'id', 'name',
          'age', 'height', 'weight',
          'joinSchoolTime', 'guardian',
          'schoolType', 'sex', 'motherPhoneNumber', 'fatherPhoneNumber',
          'motherWxchatNumber', 'fatherWechat'
        ]

      })
      return { code: 0, msg: '', list }
    } catch (error) {
      console.error(error)
      return { code: 4005, msg: 'query error [ studentList ]', list: [] }
    }
  }

  public async getStudentById(id: number): Promise<CheckResultInter & { student?: Student }> {
    try {
      // take => limit; skip => offset
      const student = await getRepository(Student).findOne({ id })
      if (!student) {
        return { code: 4004, msg: '系统档案中没有该名学生资料' }
      }
      return { code: 0, msg: '', student }
    } catch (error) {
      console.error(error)
      return { code: 4005, msg: 'query error [ getStudentById ]' }
    }
  }

  public async updateStudentInfo(id: number, data: Partial<Student>): Promise<CheckResultInter> {
    try {
      await getRepository(Student).update({ id }, data)
      return { code: 0, msg: '' }
    } catch (error) {
      console.error(error)
      return { code: 4006, msg: 'update error [ updateStudentInfo ]' }
    }
  }

  public async createStudentDetail(studentId: number, detailData: StudentDetail): Promise<CheckResultInter & { id?: number }> {
    const detail = new StudentDetail()
    detail.studentId = studentId
    detail.evaluate = detailData.evaluate || ''
    detail.picturePhotos = detailData.picturePhotos
    detail.pictureVideos = detailData.pictureVideos
    try {
      const { id } = await getRepository(StudentDetail).save(detail)
      return { msg: '', code: 0, id }
    } catch (error) {
      console.error(error)
      return { code: 4005, msg: 'insert error [ createStudent ]' }
    }
  }

  public async getStudentDetailById(studentId: number): Promise<CheckResultInter & { detailList?: StudentDetail[] }> {
    try {
      const detailList = await getRepository(StudentDetail).find({ studentId })
      if (!detailList) {
        return { code: 4004, msg: '数据不存在' }
      }
      return { code: 0, msg: '', detailList }
    } catch (error) {
      console.error(error)
      return { code: 4005, msg: 'query error [ getStudentDetailById ]' }
    }

  }
}
